package com.yj.oa.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yj.common.responses.OAResponse;
import com.yj.oa.dto.oa.ContractTransferDto;
import com.yj.oa.dto.oa.UpdateStatusDto;
import com.yj.oa.entity.system.OAContract;
import com.yj.oa.entity.system.OADepartmentConfig;
import com.yj.oa.entity.system.OAUserConfig;
import com.yj.oa.service.OAInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/4
 * Author     : Unbelievable9
 **/
@Api(tags = "易煤可视化-OA对接")
@Controller
@RequestMapping("/oa/info")
public class OAInfoController {

    private static final Gson gson = new Gson();

    @Autowired
    private OAInfoService oaInfoService;


    @ApiOperation("用户信息推送")
    @PostMapping(value = "/user-config-push", consumes = MediaType.TEXT_HTML_VALUE, produces = MediaType.TEXT_HTML_VALUE)
    public void userConfigPush(@RequestBody String userConfig,
                               HttpServletResponse response) throws IOException {
        Type userConfigType = new TypeToken<OAUserConfig>() {
        }.getType();
        OAResponse<Object> oaResponse;

        try {
            String userConfigJson = new String(Base64Utils.decode(userConfig.getBytes()), "UTF-8");
            OAUserConfig oaUserConfig = gson.fromJson(userConfigJson, userConfigType);

            oaInfoService.pushOAUserConfig(oaUserConfig);

            oaResponse = new OAResponse<>(1, "用户角色信息推送成功", null);
        } catch (Exception e) {
            e.printStackTrace();

            oaResponse = new OAResponse<>(2, "用户角色信息推送失败", null);
        }

        response.getOutputStream().write(Base64Utils.encode(gson.toJson(oaResponse).getBytes("UTF-8")));
    }

    @ApiOperation("用户信息删除")
    @PostMapping(value = "/user-config-delete", consumes = MediaType.TEXT_HTML_VALUE, produces = MediaType.TEXT_HTML_VALUE)
    public void userConfigDelete(@RequestBody String userConfig,
                               HttpServletResponse response) throws IOException {
        Type mapType = new TypeToken<Map<String, Object>>() {
        }.getType();
        OAResponse<Object> oaResponse;

        try {
            String userConfigJson = new String(Base64Utils.decode(userConfig.getBytes()), "UTF-8");
            Map<String, Object> userConfigMap = gson.fromJson(userConfigJson, mapType);

            @SuppressWarnings("unchecked")
            List<String> userIDList = (List<String>) userConfigMap.get("user_ids");

            oaInfoService.deleteOAUserConfig(userIDList);

            oaResponse = new OAResponse<>(1, "用户角色信息删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();

            oaResponse = new OAResponse<>(2, "用户角色信息删除失败", null);
        }

        response.getOutputStream().write(Base64Utils.encode(gson.toJson(oaResponse).getBytes("UTF-8")));
    }

    @ApiOperation("组织结构信息推送")
    @PostMapping(value = "/department-config-push", consumes = MediaType.TEXT_HTML_VALUE, produces = MediaType.TEXT_HTML_VALUE)
    public void departmentConfigPush(@RequestBody String departmentConfig,
                                     HttpServletResponse response) throws IOException {
        Type departmentConfigType = new TypeToken<OADepartmentConfig>() {
        }.getType();
        OAResponse<Object> oaResponse;

        try {
            String departmentConfigJson = new String(Base64Utils.decode(departmentConfig.getBytes()), "UTF-8");
            OADepartmentConfig oaDepartmentConfig = gson.fromJson(departmentConfigJson, departmentConfigType);

            oaInfoService.pushOADepartmentConfig(oaDepartmentConfig);

            oaResponse = new OAResponse<>(1, "用户角色信息推送成功", null);
        } catch (Exception e) {
            e.printStackTrace();

            oaResponse = new OAResponse<>(2, "用户角色信息推送失败", null);
        }

        response.getOutputStream().write(Base64Utils.encode(gson.toJson(oaResponse).getBytes("UTF-8")));
    }

    @ApiOperation("组织结构信息删除")
    @PostMapping(value = "/department-config-delete", consumes = MediaType.TEXT_HTML_VALUE, produces = MediaType.TEXT_HTML_VALUE)
    public void departmentConfigDelete(@RequestBody String departmentConfig,
                                     HttpServletResponse response) throws IOException {
        Type mapType = new TypeToken<Map<String, Object>>() {
        }.getType();
        OAResponse<Object> oaResponse;

        try {
            String departmentConfigJson = new String(Base64Utils.decode(departmentConfig.getBytes()), "UTF-8");
            Map<String, Object> departmentConfigMap = gson.fromJson(departmentConfigJson, mapType);

            @SuppressWarnings("unchecked")
            List<Long> departmentIDList = (List<Long>) departmentConfigMap.get("department_ids");

            oaInfoService.deleteDepartmentConfig(departmentIDList);

            oaResponse = new OAResponse<>(1, "用户角色信息删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();

            oaResponse = new OAResponse<>(2, "用户角色信息删除失败", null);
        }

        response.getOutputStream().write(Base64Utils.encode(gson.toJson(oaResponse).getBytes("UTF-8")));
    }

    @ApiOperation("合同信息推送")
    @PostMapping(value = "/contract-push", consumes = MediaType.TEXT_HTML_VALUE, produces = MediaType.TEXT_HTML_VALUE)
    public void contractPush(@RequestBody String contract,
                             HttpServletResponse response) throws IOException {
        Type contractType = new TypeToken<OAContract>() {
        }.getType();
        OAResponse<Object> oaResponse;

        try {
            String contractJson = new String(Base64Utils.decode(contract.getBytes()), "UTF-8");
            OAContract oaContract = gson.fromJson(contractJson, contractType);

            Integer count = oaInfoService.contractCountForMainID(oaContract.getMain_id());

            if (count > 0) {
                oaInfoService.updateContract(oaContract);
            } else {
                oaInfoService.addContract(oaContract);
            }

            oaResponse = new OAResponse<>(1, "合同信息推送成功", null);
        } catch (Exception e) {
            e.printStackTrace();

            oaResponse = new OAResponse<>(0, "合同信息推送失败", null);
        }

        response.getOutputStream().write(Base64Utils.encode(gson.toJson(oaResponse).getBytes("UTF-8")));
    }

    @ApiOperation("合同状态推送")
    @PostMapping(value = "/contract-status-update", consumes = MediaType.TEXT_HTML_VALUE, produces = MediaType.TEXT_HTML_VALUE)
    public void contractStatusUpdate(@RequestBody String status,
                                     HttpServletResponse response) throws IOException {
        Type statusType = new TypeToken<UpdateStatusDto>() {
        }.getType();
        OAResponse<Object> oaResponse;

        try {
            String statusJson = new String(Base64Utils.decode(status.getBytes()), "UTF-8");
            UpdateStatusDto oaUpdateStatusDto = gson.fromJson(statusJson, statusType);

            oaInfoService.updateContractStatus(oaUpdateStatusDto);

            oaResponse = new OAResponse<>(1, "合同状态推送成功", null);
        } catch (Exception e) {
            e.printStackTrace();

            oaResponse = new OAResponse<>(0, "合同状态推送失败", null);
        }

        response.getOutputStream().println(new String(Base64Utils.encode(gson.toJson(oaResponse).getBytes("UTF-8"))));
    }

    @ApiOperation("合同转让推送")
    @PostMapping(value = "/contract-transfer", consumes = MediaType.TEXT_HTML_VALUE, produces = MediaType.TEXT_HTML_VALUE)
    public void contractTransfer(@RequestBody String transfer,
                                 HttpServletResponse response) throws IOException {
        Type transferType = new TypeToken<ContractTransferDto>() {
        }.getType();
        OAResponse<Object> oaResponse;
        Map<String, Object> infoMap;

        try {
            String transferJson = new String(Base64Utils.decode(transfer.getBytes()), "UTF-8");
            ContractTransferDto contractTransferDto = gson.fromJson(transferJson, transferType);

            infoMap = oaInfoService.transferContract(contractTransferDto);

            if (infoMap != null) {
                Integer status = (Integer) infoMap.get("status");
                String message = (String) infoMap.get("message");

                oaResponse = new OAResponse<>(status, message, null);
            } else {
                oaResponse = new OAResponse<>(1, "合同转让成功", null);
            }
        } catch (Exception e) {
            e.printStackTrace();

            oaResponse = new OAResponse<>(0, "合同转让失败", null);
        }

        response.getWriter().println(new String(Base64Utils.encode(gson.toJson(oaResponse).getBytes("UTF-8"))));
    }
}